package com.sangcomz.fishbun.adapter.image.impl;

import com.sangcomz.fishbun.adapter.image.ImageAdapter;
import com.sangcomz.fishbun.util.MainUiHandler;
import com.sangcomz.fishbun.util.UiHandler;
import com.sangcomz.fishbun.util.ThreadPoolUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.agp.components.Image;
import ohos.media.image.ImagePacker;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.ImageInfo;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;
import ohos.utils.net.Uri;

import java.io.*;

/**
 * Created by sangcomz on 23/07/2017.
 */

public class GlideAdapter implements ImageAdapter {

    private UiHandler uiHandler = new MainUiHandler();

    @Override
    public void loadImage(AbilitySlice abilitySlice, Image target, Uri loadUrl) {

        ThreadPoolUtil.execute(new Runnable() {
            @Override
            public void run() {
                ImageSource imageSource = null;
                DataAbilityHelper helper = null;

                try {
                    helper = DataAbilityHelper.creator(abilitySlice);
                    FileDescriptor fileDescriptor = helper.openFile(loadUrl, "r");
                    imageSource = ImageSource.create(fileDescriptor, null);
                    ImageInfo imageInfo = imageSource.getImageInfo();
                    Size size = imageInfo.size;

                    ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();

                    decodingOpts.desiredSize = sampleSize(target, size);

                    PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true);

                    uiHandler.run(new UiHandler.RunUIThread() {
                        @Override
                        public void run() {
                            target.setPixelMap(pixelMap);
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (imageSource != null) {
                        imageSource.release();
                    }
                    if (helper != null) {
                        helper.release();
                    }
                }
            }
        });
    }

    @Override
    public void loadDetailImage(AbilitySlice abilitySlice, Image target, Uri loadUrl) {
        ThreadPoolUtil.threadPool().execute(new Runnable() {
            @Override
            public void run() {
                ImageSource imageSource = null;
                DataAbilityHelper helper = null;

                try {
                    helper = DataAbilityHelper.creator(abilitySlice);
                    FileDescriptor fileDescriptor = helper.openFile(loadUrl, "r");
                    imageSource = ImageSource.create(fileDescriptor, null);
                    ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
                    Size size = imageSource.getImageInfo().size;
                    int targetWidth = target.getWidth();
                    if (targetWidth == 0){
                        targetWidth = 1080;
                    }
                    int ratio = size.width / targetWidth;
                    if (ratio > 1) {//按照压缩比进行压缩
                        decodingOpts.desiredSize = new Size(size.width / ratio, size.height / ratio);
                    } else {//不需要进行压缩
                        decodingOpts.desiredSize = size;
                    }
                    PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true);

                    uiHandler.run(() -> target.setPixelMap(pixelMap));

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (imageSource != null) {
                        imageSource.release();
                    }
                    if (helper != null) {
                        helper.release();
                    }
                }

            }
        });
    }

    public Size sampleSize(Image image, Size resSize) {
        int imageWidth = image.getWidth() > 0 ? image.getWidth() : 200;
        int imageHeight = image.getHeight() > 0 ? image.getHeight() : 200;

        int resourceWidth = resSize.width;
        int resourceHeight = resSize.height;

        int inSampleSize = Math.min(resourceWidth / imageWidth, resourceHeight / imageHeight);
        if (inSampleSize <= 1) {
            return resSize;
        } else {
            return new Size(resourceWidth / inSampleSize, resourceHeight / inSampleSize);
        }

    }
}
